//
//  ZDPresentationController.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/11/9.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

let ZDPopoverViewControllerDismissNotification = "ZDPopoverViewControllerDismissNotification"

class ZDPresentationController: UIPresentationController {

    /*
        presentingViewController
    
        presentedViewController   //执行动画的view所属的控制器  modal出来的控制器
    
        containerView    //容器视图  本质上是UITransitionView  存放modal出来的控制器的view
    
        containerViewWillLayoutSubviews()
    
        containerViewDidLayoutSubviews()
    
        presentedView()  //返回执行动画的view   modal出来的控制器的view
    
    */
    
    
    //返回执行动画的view的frame ———— 不能在这里设置modal view的frame  否则在加上动画时显示异常
//    override func frameOfPresentedViewInContainerView() -> CGRect {
//        
//        return CGRect(x: 100, y: 56, width: 200, height: 250)
//    }
    
    
    override func containerViewWillLayoutSubviews() {
        
        //调用父类的方法
        super.containerViewDidLayoutSubviews()
        
        print("containerViewWillLayoutSubviews")  //测试该方法的执行次数
        
        //设置执行动画的view的frame
        presentedView()?.frame = CGRect(x: 100, y: 56, width: 200, height: 250)
        
        //设置灰蒙蒙的背景色
        containerView?.backgroundColor = UIColor (white: 0, alpha: 0.2)
        
        //创建手势
        let tap = UITapGestureRecognizer(target: self, action: "tap")
        
        //添加手势
        containerView?.addGestureRecognizer(tap)
    }
    
    //在containerViewDidLayoutSubviews方法中执行如下代码  在dismiss动画时 会有拉伸的bug
    override func containerViewDidLayoutSubviews() {
        
//        //调用父类的方法
//        super.containerViewDidLayoutSubviews()
//        
//        print("containerViewDidLayoutSubviews")  //测试该方法的执行次数
//        
//        //设置执行动画的view的frame
//        presentedView()?.frame = CGRect(x: 100, y: 56, width: 200, height: 250)
//        
//        //设置灰蒙蒙的背景色
//        containerView?.backgroundColor = UIColor (white: 0, alpha: 0.2)
//        
//        //创建手势
//        let tap = UITapGestureRecognizer(target: self, action: "tap")
//        
//        //添加手势
//        containerView?.addGestureRecognizer(tap)
    }
    
    ///点击手势
    func tap() {
    
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
        
        //通知旋转按钮
    NSNotificationCenter.defaultCenter().postNotificationName("ZDPopoverViewControllerDismissNotification", object: self)
    }
    
}